﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DevExpress.Web.ASPxTreeList;
using System.Data.SqlClient;

using DAL;
using System.Collections;
public partial class BasicInfo_DepartRy : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        //显示数据treelist
        // base.Page_Load(sender, e);
        tvShow();
    }
    //protected override string ActionID
    //{
    //    get { return "0"; }
    //}
    void tvShow()
    {
        DataTable dt = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable("select * from css_department");
        this.DxTreelist.DataSource = dt;
        this.DxTreelist.DataBind();

    }

    //实现选择不同的人员显示部门信息
    protected void DxTreelist_CustomCallback(object sender, DevExpress.Web.ASPxTreeList.TreeListCustomCallbackEventArgs e)
    {
        string[] bmbms;
        string bmbm = "";
        string rybm = e.Argument.ToString();
        string strSql = "select bmbm from css_department_rybm where rybm='" + rybm + "'";
        using (DataClassesDataContext lqdb = new DataClassesDataContext())
        {
            var result = from r in lqdb.css_department_rybm
                         where r.rybm == rybm
                         select r;
            foreach (var item in result)
            {
                bmbm = bmbm + item.bmbm + '|';
            }
        }
        bmbms = bmbm.Split('|');

        //清除所有选择    
        for (int i = 0; i < this.DxTreelist.Nodes.Count; i++)
        {
            this.DxTreelist.Nodes[i].Selected = false;
        }
        //查找是否有存在的部门
        if (bmbms.Length > 0)
        {
            for (int i = 0; i < bmbms.Length; i++)
            {
                if (!bmbms[i].Equals(""))
                {
                    TreeListNode selection = DxTreelist.FindNodeByKeyValue(bmbms[i]); //查找部门
                    //如果有则设置为true
                    if (selection != null)
                    {
                        selection.Selected = true;
                    }
                }
            }
        }
    }

    //实现修改并保存当前人员的权限信息
    protected void QueryCBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        //得到从客户端传递过来的参数来进行异步通信响应
        if (e.Parameter.Equals("null"))
        {
            throw new Exception("请选择人员！");
        }
        string bmbm = "";
        if (this.DxTreelist.GetSelectedNodes().Count > 0)   //判断treelist是否勾选了部门信息
        {
            List<TreeListNode> selection = this.DxTreelist.GetSelectedNodes();   //获取选定的部门信息
            //遍历node并连接成字符串
            foreach (TreeListNode node in selection)
            {
                bmbm = bmbm + node.GetValue("bmbm").ToString() + '|';
            }
        }
        string[] bmbms;
        bmbms = bmbm.Split('|');
        using (DataClassesDataContext lqDB = new DataClassesDataContext())
        {
            string[] result = lqDB.css_department_rybm.Where(r => r.rybm == e.Parameter).Select(u => u.bmbm).ToArray();
            //判断数据库中的数据和用户选择的部门是否相同
            string str = IsNotSame(bmbms, result);
            if (!str.Equals(""))
            {
                var delitem = lqDB.css_department_rybm.FirstOrDefault(o => o.bmbm == str && o.rybm == e.Parameter);
                if (delitem != null)
                {
                    lqDB.css_department_rybm.DeleteOnSubmit(delitem);
                    lqDB.SubmitChanges();
                }
            }

        }
        //保存勾选的部门信息
        using (DataClassesDataContext lqdb = new DataClassesDataContext())
        {
            if (bmbms.Length > 0)
            {
                for (int i = 0; i < bmbms.Length; i++)    //遍历用户选择的部门编号
                {
                    if (!bmbms[i].Equals(""))
                    {
                        var partry = lqdb.css_department_rybm.FirstOrDefault(d => d.bmbm == bmbms[i] && d.rybm == e.Parameter);  //检查是否已经存在
                        if (partry == null)
                        {
                            css_department_rybm d = new css_department_rybm();
                            d.rybm = e.Parameter;
                            d.bmbm = bmbms[i].ToString();
                            lqdb.css_department_rybm.InsertOnSubmit(d);
                        }
                    }

                }
            }
            lqdb.SubmitChanges();

        }
    }

    //找出两个数据中不同的元素
    string IsNotSame(string[] a, string[] s)
    {
        string str = "";
        for (int i = 0; i < s.Length; i++)
        {
            bool isEqual = false;
            for (int j = 0; j < a.Length; j++)
            {
                if (s[i] == a[j])
                {
                    isEqual = true;
                    break;
                }
                else
                {
                    isEqual = false;
                }
            }

            if (!isEqual)
            {
                str = s[i];
            }
        }
        return str;
    }
}